#include <QtGui>
#include "MainWindow.h"
#include "myglwidget.h"
#include "scenebasic.h"
#include "glutils.h"
#include "parameterwidget.h"

QPlainTextEdit* MainWindow::textEdit = NULL;

MainWindow::MainWindow(void)
{
    //the GL view
    QGLFormat format;
    format.setVersion(4, 0);
	format.setProfile(QGLFormat::CompatibilityProfile);
    glView = new MyGLWidget(format);

    setCentralWidget(glView);

	createActions();
	createMenus();
	createToolBars();
	createStatusBar();
	createDockWindows();

	readSettings();

	setCurrentFile("");
	setUnifiedTitleAndToolBarOnMac(true);
}

MainWindow::~MainWindow()
{
}

void MainWindow::createDockWindows()
{
    //--log dock
	QDockWidget *logDock = new QDockWidget(tr("Log"), this);
	textEdit = new QPlainTextEdit;
	textEdit->setReadOnly(true);
	logDock->setWidget(textEdit);
	addDockWidget(Qt::BottomDockWidgetArea, logDock);

    //--feature dock
	QDockWidget *featureDock = new QDockWidget(tr("Features"), this);
    QWidget *ftWidget = new ParameterWidget(glView);
    featureDock->setWidget(ftWidget);
	addDockWidget(Qt::RightDockWidgetArea, featureDock);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
	writeSettings();
	event->accept();
}

void MainWindow::open()
{
	QString fileName = QFileDialog::getOpenFileName(this);
	if (!fileName.isEmpty())
		loadFile(fileName);
}

void MainWindow::about()
{
	QMessageBox::about(this, tr("About Application"),
		tr("The <b>Application</b> example demonstrates how to "
		"write modern GUI applications using Qt, with a menu bar, "
		"toolbars, and a status bar."));
}

void MainWindow::pause()
{
	if (!isPause)
	{
		glView->pauseAnimation();
		pauseAct->setIcon(QIcon(":/images/play.png"));
	}
	else
	{
		glView->playAnimation();
		pauseAct->setIcon(QIcon(":/images/pause.png"));
	}
	isPause = !isPause;
}

void MainWindow::reset()
{
	glView->resetAnimation();
}

void MainWindow::createActions()
{
	openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
	openAct->setShortcuts(QKeySequence::Open);
	openAct->setStatusTip(tr("Open an existing file"));
	connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

	isPause = false;
	pauseAct = new QAction(QIcon(":/images/pause.png"), tr("&Play/Pause..."), this);
	pauseAct->setStatusTip(tr("Pause the animation"));
	connect(pauseAct, SIGNAL(triggered()), this, SLOT(pause()));

	resetAct = new QAction(QIcon(":/images/reset.png"), tr("&Reset data..."), this);
	resetAct->setStatusTip(tr("Reset the smoke data"));
	connect(resetAct, SIGNAL(triggered()), this, SLOT(reset()));

	exitAct = new QAction(tr("E&xit"), this);
	exitAct->setShortcuts(QKeySequence::Quit);
	exitAct->setStatusTip(tr("Exit the application"));
	connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));

	aboutAct = new QAction(tr("&About"), this);
	aboutAct->setStatusTip(tr("Show the application's About box"));
	connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));

	aboutQtAct = new QAction(tr("About &Qt"), this);
	aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
	connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}

void MainWindow::createMenus()
{
	fileMenu = menuBar()->addMenu(tr("&File"));
	fileMenu->addAction(openAct);
	fileMenu->addSeparator();
	fileMenu->addAction(exitAct);

	menuBar()->addSeparator();

	helpMenu = menuBar()->addMenu(tr("&Help"));
	helpMenu->addAction(aboutAct);
	helpMenu->addAction(aboutQtAct);
}

void MainWindow::createToolBars()
{
	fileToolBar = addToolBar(tr("File)"));
	fileToolBar->addAction(openAct);
	fileToolBar->addAction(pauseAct);
	fileToolBar->addAction(resetAct);
}

void MainWindow::createStatusBar()
{
	statusBar()->showMessage(tr("Ready"));
}

void MainWindow::readSettings()
{
    QSettings settings("Ajou univ.", "Prototype for Research");
	QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
	QSize size = settings.value("size", QSize(400, 400)).toSize();
	resize(size);
	move(pos);
}

void MainWindow::writeSettings()
{
	QSettings settings("Trolltech", "Application Example");
	settings.setValue("pos", pos());
	settings.setValue("size", size());
}

void MainWindow::loadFile(const QString &fileName)
{
#ifndef QT_NO_CURSOR
	QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
    bool iLoaded = reinterpret_cast<SceneBasic*>(glView->getScene())->loadmesh(fileName.toLatin1().data());        //load file here
#ifndef QT_NO_CURSOR
	QApplication::restoreOverrideCursor();
#endif

	if (iLoaded)
	{
		setCurrentFile(fileName);
		statusBar()->showMessage(tr("File loaded"), 2000);
        QString log = tr("Mesh file:") + fileName + "\n";
		textEdit->appendPlainText(log);
    }
    else 		//report error;
	{
		QMessageBox::critical(this, tr("Mesh Cut"), tr("The program got an error loading the file"));
	}
}

void MainWindow::setCurrentFile(const QString &fileName)
{
	curFile = fileName;

	QString shownName = curFile;
	if (curFile.isEmpty())
		shownName = "untitled.txt";
	setWindowFilePath(shownName);
}

QString MainWindow::strippedName(const QString &fullFileName)
{
	return QFileInfo(fullFileName).fileName();
}
